package com.tb.foryou;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class MainActivity extends AppCompatActivity {
    private MainView mainView;

    //时间未到
    private static final int DAY_BEFORE = -1;
    //时间已过
    private static final int DAY_AFTER = -2;

    //时间格式
    private SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
    //存储每日语句
    private String[] book;
    //记录现在的天数
    private int dayToStart;
    //按钮
    private Button buttonHead;
    private Button buttonNow;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //获取每日语句
        book = getResources().getStringArray(R.array.books);
        //初始化mainView
        mainView = (MainView) findViewById(R.id.main_view);
        dayToStart = getDayToStart();
        mainView.init(book,dayToStart);
        //初始化按钮
        buttonHead = (Button) findViewById(R.id.button_head);
        buttonHead.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mainView.goHeadPage();
            }
        });
        buttonNow = (Button) findViewById(R.id.button_now);
        buttonNow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mainView.goNowPage();
            }
        });
    }

    private int getDayToStart(){
        //起始日期
        Date startDate = null;
        try {
            startDate = df.parse(getResources().getString(R.string.start_date));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //现在日期
        Date nowDate = new Date();
        //记录现在的天数
        int dayToStart;
        //获取天数

        dayToStart = (int) ((nowDate.getTime()-startDate.getTime())/(24*60*60*1000));
        //调整天数
        if((nowDate.getTime()-startDate.getTime()) < 0){
            dayToStart = DAY_BEFORE;
        }
//        if(dayToStart > book.length){
//            dayToStart = DAY_AFTER;
//        }
        Log.d("startDate","" + startDate);
        Log.d("nowDate","" + nowDate);
        Log.d("dayToStart","" + dayToStart);
        return dayToStart;
    }





}
